<script setup>
import { ref } from 'vue'
import { artGetChannelsService,artDelChannelService } from '@/api/article';
import { Edit, Delete } from '@element-plus/icons-vue'
import ChannelEdit from './components/ChannelEdit.vue';

const channelList = ref([])
const loading = ref(false)
const dialog = ref(false)

const getChannels = async () => {
  loading.value = true
  const res = await artGetChannelsService()
  channelList.value = res.data.data
  console.log(channelList.value);
  loading.value = false
}
getChannels()

const onEditChannel = (row) => {
  dialog.value.open(row)
}

const onAddChannel = () => {
  dialog.value.open({})
}

const onSuccess = () => {
  getChannels()
}

const onDeleteChannel = async (row) => {
  await ElMessageBox.confirm('你确认删除该分类信息吗？', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await artDelChannelService(row.id)
  ElMessage({ type: 'success', message: '删除成功' })
  getChannels()
}

</script>
<template>
  <page-container title="文章分类">
    <template #extra>
      <el-button @click="onAddChannel">添加分类</el-button>
    </template>

    <el-table :data="channelList" style="width: 100%" v-loading="loading">
      <el-table-column type="index" prop="id" label="序号" wdith="100"></el-table-column>
      <el-table-column prop="cate_name" label="分类名称"></el-table-column>
      <el-table-column prop="cate_alias" label="分类别名"></el-table-column>
      <el-table-column label="操作" width="100">
        <template #default="{ row, $index }">
          <el-button @click="onEditChannel(row, $index)" :icon="Edit" circle type="primary" plain></el-button>
          <el-button @click="onDeleteChannel(row, $index)" :icon="Delete" circle type="danger" plain></el-button>
        </template>
      </el-table-column>

      <template #empty>
        <el-empty description="暂无数据"></el-empty>
      </template>

    </el-table>

    <channel-Edit ref="dialog" @success="onSuccess"></channel-Edit>
  </page-container>
</template>

<style lang="scss" scoped></style>
